<?php /** @noinspection DuplicatedCode */
/**
 * 导出宜家新年活动数据
 * 每天上午10点发送头一天的数据
 * 需要记录 完成每个任务的 uid、昵称
 *需要记录 中奖的uid、昵称及对应填写的中奖信息
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
error_reporting(E_ERROR | E_PARSE);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\IkeaActivity\IkeaActivityJoinRecordModel;
use Biz\model\IkeaActivity\IkeaActivityWinnerListModel;
use Biz\pockets\excel\DownExcel;
use Biz\tools\Mail;
use Doraemon\service\User;
use Doraemon\tools\Log;


const EMAIL_MAP = [
    'renzhifan@haohaozhu.com',
    'lixiaojie@haohaozhu.com',
];

try {
    $startTime = $argv[1] ?? date("Y-m-d", strtotime("-1 day", time()));
    $endTime = $argv[2] ?? date('Y-m-d H:i:s', strtotime($startTime) + 86399);
    if(date('Ymd') > 20221229){
        sendJoinList($startTime, $endTime);
        sendWinnerList($startTime, $endTime);
    }else{
        echo '活动未开始'.PHP_EOL;
    }

} catch (Exception $exception) {
    Log::error('ExportIkeaActivity.php', $exception);
}

function sendJoinList($startTime, $endTime)
{
    $where = [
        'addtime[>=]' => $startTime,
        'addtime[<=]' => $endTime,
        'type' => IkeaActivityJoinRecordModel::TYPE_TASK,
        'status' => IkeaActivityJoinRecordModel::STATUS_NORMAL,
    ];
    $joinList = IkeaActivityJoinRecordModel::selectByWhere($where);
    if (!empty($joinList)) {
        $joinListUidArr = array_column($joinList, 'uid');
        $userList = User::getBatchBasicUserInfo($joinListUidArr);
        $userList = array_column($userList, null, 'uid');
        $taskMap = array_column(IkeaActivityJoinRecordModel::TASK_MAP, null, 'task_id');
        $list = [];
        foreach ($joinList as $join) {
            $list[] = [
                'task_name' => $taskMap[$join['task_id']]['task_name'] ?? '',
                'uid' => $join['uid'],
                'nick' => $userList[$join['uid']]['nick'] ?? '',
                'addtime' => $join['addtime'],
            ];
        }
        $filePath = exportJoinList($list);
        $pageTitle = '宜家新年活动-任务完成记录';
        Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$startTime | $endTime", $filePath);
        echo '发送邮件成功'.PHP_EOL;
    }

}

function sendWinnerList($startTime, $endTime)
{
    $where = [
        'addtime[>=]' => $startTime,
        'addtime[<=]' => $endTime,
        'status' => IkeaActivityWinnerListModel::STATUS_NORMAL,
    ];
    $winnerList = IkeaActivityWinnerListModel::selectByWhere($where);
    if (!empty($winnerList)) {
        $winnerListUidArr = array_column($winnerList, 'uid');
        $userList = User::getBatchBasicUserInfo($winnerListUidArr);
        $userList = array_column($userList, null, 'uid');
        $list = [];
        foreach ($winnerList as $winner) {
            $list[] = [
                'uid' => $winner['uid'],
                'nick' => $userList[$winner['uid']]['nick'] ?? '',
                'name' => $winner['name'],
                'phone' => $winner['phone'],
                'address' => $winner['address'],
                'addtime' => $winner['addtime'],
            ];
        }
        $filePath = exportWinnerList($list);
        $pageTitle = '宜家新年活动-中奖记录';
        Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$startTime | $endTime", $filePath);
        echo '发送邮件成功'.PHP_EOL;
    }

}

//导出完成任务名单
function exportJoinList($list)
{
    $header = [
        '任务名称',
        'uid',
        '昵称',
        '完成时间',
    ];
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName('宜家新年活动-任务完成记录'.date('Y-m-d-H-i-s'));
    $excel_obj->setSheetTitle('宜家新年活动-任务完成记录');

    // 设置列宽
    $widths = [
        'A' => 30,
        'B' => 30,
        'C' => 30,
        'D' => 30,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();

    return ($excel_obj->create($list, $header));
}

//导出中奖名单
function exportWinnerList($list)
{
    $header = [
        'uid',
        '昵称',
        '姓名',
        '手机号',
        '收货地址',
        '中奖时间',
    ];
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName('宜家新年活动-中奖记录'.date('Y-m-d-H-i-s'));
    $excel_obj->setSheetTitle('宜家新年活动-中奖记录');

    // 设置列宽
    $widths = [
        'A' => 30,
        'B' => 30,
        'C' => 30,
        'D' => 30,
        'E' => 100,
        'F' => 30,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();

    return ($excel_obj->create($list, $header));
}
